package com.cgwx.service.impl;

import com.cgwx.config.BusinessException;
import com.cgwx.entity.Favorites;
import com.cgwx.entity.File;
import com.cgwx.entity.Folder;
import com.cgwx.entity.Room;
import com.cgwx.mapper.FavoritesMapper;
import com.cgwx.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class FavoritesServiceImpl implements IFavoritesService {
    @Resource
    private FavoritesMapper favoritesMapper;

    @Resource
    private IFolderService folderService;

    @Resource
    private IFileService fileService;

    @Resource
    private IRoomService roomService;

    @Resource
    private IAuthorityService authorityService;

    @Override
    public List<Favorites> getAllFavorites() {
        return favoritesMapper.getAllFavorites();
    }

    @Override
    public List<Favorites> getFavorites(String account, String deptId, String pattern, String column, String sort) {
        Favorites favorites = new Favorites();
        favorites.setAccount(account);
        favorites.setEntityName(pattern);
        favorites.setColumn(column);
        favorites.setSort(sort);
        List<Favorites> favoritesList =  favoritesMapper.getFavorites(favorites);
        List<Favorites> favoritesList1 =new ArrayList<>();
        for (Favorites favorite : favoritesList) {
            if(!favorite.getRoomId().startsWith("free")) favoritesList1.add(favorite);
        }
        favoritesList = favoritesList1;
        Room room = new Room();
        for(Favorites favorite: favoritesList) {
            room.setId(favorite.getRoomId());
            String roomType = roomService.getRoom(room).get(0).getRoomType();
            if(roomType.equals("staff")) favorite.setAuthorityLevel("4");
            else favorite.setAuthorityLevel(authorityService.checkAuthority(account, deptId, favorite.getEntityId()));
        }
        return favoritesList;
    }

    @Override
    @Transactional
    public String deleteFavorites(String account, List<String> entityIds) {
        for(String entityId: entityIds) {
            System.out.println(entityId);
            System.out.println(account);
            boolean result = favoritesMapper.deleteFavoritesByAccountAndId(account, entityId);
            if (!result){
                log.error("取消收藏失败");
                throw new BusinessException("取消收藏失败");
            }
        }
        return "取消收藏成功!";
    }

    @Override
    @Transactional
    public String insertFavorites(String account, List<String> entityIds) {
        Favorites favorites = new Favorites();
        for(String entityId: entityIds){
            favorites.setAccount(account);
            favorites.setEntityId(entityId);
            if(entityId.startsWith("folder")) {
                Folder folder = new Folder();
                folder.setEntityId(entityId);
                List<Folder> folders = folderService.getFolder(folder);
                if (folders.isEmpty()){
                    log.error("没有对应该entityId的文件夹");
                    throw new BusinessException("没有对应该entityId的文件夹!");
                }
                else if(folders.size() > 1){
                    log.error("entityId不唯一");
                    throw new BusinessException("entityId不唯一!");
                }
                folder = folders.get(0);
                favorites.setFileOrFolder("folder");
                favorites.setEntityName(folder.getEntityName());
                favorites.setOwnerAccount(folder.getOwnerAccount());
                favorites.setOwnerName(folder.getOwnerName());
                favorites.setType(folder.getType());
                favorites.setTag(folder.getTag());
                favorites.setIsDeleted("false");
                favorites.setRoomId(folder.getEntityRoom());
                favorites.setCreateTime(folder.getCreateTime());
                favorites.setUpdateTime(folder.getUpdateTime());
            }
            else if(entityId.startsWith("file")) {
                File file = new File();
                file.setEntityId(entityId);
                List<File> files = fileService.getFile(file);
                if (files.isEmpty()){
                    log.error("没有对应该entityId的文件!");
                    throw new BusinessException("没有对应该entityId的文件!");
                }
                else if(files.size() > 1) {
                    log.error("entityId不唯一!");
                    throw new BusinessException("entityId不唯一!");
                }
                file = files.get(0);
                favorites.setFileOrFolder("file");
                favorites.setEntityName(file.getEntityName());
                favorites.setOwnerAccount(file.getOwnerAccount());
                favorites.setOwnerName(file.getOwnerName());
                favorites.setType(file.getType());
                favorites.setTag(file.getTag());
                favorites.setFileUrl(file.getFileUrl());
                favorites.setFileId(file.getFileId());
                favorites.setIsDeleted("false");
                favorites.setRoomId(file.getEntityRoom());
                favorites.setCreateTime(file.getCreateTime());
                favorites.setUpdateTime(file.getUpdateTime());
            }
            else{
                log.error("entityId错误!");
                throw new BusinessException("entityId错误!");
            }
            favorites.setId(generateId());
            boolean result = favoritesMapper.insertFavorites(favorites);
            if(!result){
                log.error("收藏失败!");
                throw new BusinessException("收藏失败!");
            }
        }
        return "收藏成功!";
    }
    public String generateId() {
        return UUID.randomUUID().toString();
    }
}
